package com.token.iot.bratterycharging.gateway.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.gateway.IDGeneration;
import com.token.iot.bratterycharging.gateway.api.Port;
import com.token.iot.bratterycharging.gateway.api.PortState;
import com.token.iot.bratterycharging.gateway.utils.ByteUtil;

@Component("STADuplexProcessor")
public class STADuplexProcessor extends DuplexProcessor {

	@Override
	public String request(String imei, Map<String, String> params) throws PendingException {
		String c = "STA";
		String sessionId = IDGeneration.createSeesionID();
		String command = c + sessionId + "/";
		byte[] cc = ByteUtil.addFlag(command.getBytes());
		context.writeAndFlush(cc);
		loger.info("the send =" + new String(command));
		return sessionId;
	}

	@Override
	public List<Port> response(String response) throws PendingException {
		String val = response.substring(14);

		loger.info(" the port is" + val);
		String[] ports = val.split("/");
		List<Port> list = new ArrayList();
		for (String port : ports) {
			Port p = new Port();
			String[] s = port.split(":");
			p.setNo(s[0]);
			PortState state = PortState.getByCode(s[1]);
			p.setState(state);
			list.add(p);
		}
		return list;
	}

}
